home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Mac OS SDK / Dev.CD Jan 98 SDK2.toast / Development Kits (Disc 2) / QuickDraw GX / Programming Stuff / Sample Code / Printing Samples / Applications… / All Shapes with Printing ƒ / all shapes with printing.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-27  |  10.2 KB  |  335 lines  |  [TEXT/KAHL]

  1. /*
  2.     All Shapes.c
  3.     
  4.     "All Shapes" creates, manipulates, and draws all of the QuickDraw™ GX primitive shapes. As each shape is created,
  5.     it is added to "thePage". This shape represents the contents of the window. To create a shape, the user
  6.     needs to "click" within the content region of the window.
  7.     
  8.     This file contains the calls that an application needs to make the "graphics shell" work correctly.
  9.     
  10.     ©1992 - 1993  Apple Computer, Inc.
  11.     All rights reserved.
  12.     
  13.     3/22/94 - dmh - Corrected GXSetShapeAttributes usage to preserve old attributes.
  14.     8/24/94 - dmh - Universalized.
  15. */
  16.  
  17. #include <StdIO.h>
  18.  
  19. #include <events.h>
  20. #include <memory.h>
  21. #include <windows.h>
  22.  
  23. #include "FontLibrary.h"
  24. #include "GraphicsLibraries.h"
  25. #include <GXEnvironment.h>
  26. #include <GXErrors.h>
  27. #include "QDLibrary.h"
  28. #include <GXPrinting.h>
  29.  
  30. #include "graphics shell.h"
  31.  
  32.  
  33. /**  Set up the title and size of the window  **/
  34. Str255         gWindowTitle = "\p Shapes...";
  35. Rect         gWindowQDRect  = {50, 15, 475, 575};
  36.  
  37. /** 
  38.     If gDebugging = TRUE, graphics library errors and notices will be posted.  This functionality will only work with 
  39.     the "debugging" version of the QuickDraw GX init.  If this version of the init is not installed, nothing bad will happen, 
  40.     but these  functions will not work. 
  41. **/
  42. Boolean        gDebugging = true;
  43.  
  44.  
  45. /**  Set  "gGiveMeValidation" to TRUE, if you will receive run-time validation.  **/
  46. Boolean        gGiveMeValidation = true;
  47.  
  48.  
  49. /**     
  50.     gGraphicsHeapSize sets the size of the graphics heap created by calling the GXNewGraphicsClient routine
  51.     in main () within graphics shell.c.  You can determine the amount of graphics heap required by using GraphicsBug.
  52.     I'm giving it 600K, since we need abunch if we have several windows open.
  53. **/
  54. long        gGraphicsHeapSize = 600;
  55.  
  56.  
  57. /*------ DoInitialization ---------------------------------------------------------------------------------*/
  58. //
  59. //    In this function we create and gxInitialize the the private document structure for a new window.  This
  60. //    structure contains the print job and the shape which is drawn in the window.  We store this data in
  61. //    a handle and hang it off the window's refCon field for easy retrieval.  By doing this, rather than using
  62. //    globals, we can create many windows containing unique print jobs and shapes. 
  63. //
  64. OSErr DoInitialization(wind)
  65. WindowPtr wind;
  66. {
  67.     OSErr    err;
  68.     gxJob    docJob;
  69.     gxShape    docPage;
  70.     TH_Doc    windDoc;
  71.  
  72. // Create the page shape. We set the unique items attribute to make sure that each item added to the
  73. // picture has a unique reference. If this attribute was not set, we would not see all six S's rotated.
  74. // We would only see the last "S" rotated; not all 6 pieces.        
  75.  
  76.     docPage = GXNewShape(gxPictureType);
  77.     GXSetShapeAttributes(docPage, GXGetShapeAttributes(docPage)|gxUniqueItemsShape);
  78.     
  79. // Create a print job for this document.  This will be the same as the system default until the user
  80. // goes through the dialogs for Page Setup or Print…
  81.  
  82.     err = GXNewJob(&docJob);
  83.     
  84.     
  85. // If there are no errors, create a handle the size of our document structure and store the print job and page shape
  86. // in it.  Store the handle in the window's refCon field so that we can get at it.  (Note that the utility routines
  87. // "GetDocJob" and "GetDocShape" can be used to do this easily.
  88.  
  89.     if (!err)
  90.     {
  91.         windDoc = (TH_Doc) NewHandleClear(sizeof(T_Doc));
  92.  
  93.         if (!windDoc)
  94.             err = MemError();
  95.         else
  96.         {
  97.             (*windDoc)->docJob = docJob;
  98.             (*windDoc)->docPage = docPage;
  99.             ((WindowPeek) wind)->refCon = (long) windDoc;
  100.         }
  101.  
  102. // Now install our application override for PrintingEvent so that we can
  103. // support the new movable-modal printing dialog boxes.
  104.  
  105.         GXInstallApplicationOverride(docJob, gxPrintingEventMsg, NewGXPrintingEventProc(MyPrintingEventOverride));
  106.     }
  107. }
  108.  
  109.  
  110. /*------ DoDraw ---------------------------------------------------------------------------------------*/
  111. //
  112. //    Draw the contents of the window.
  113. // 
  114. void DoDraw(wind)
  115. WindowPtr wind;
  116. {
  117.      GXDrawShape (GetDocShape(wind));
  118. }
  119.  
  120.  
  121. /*------ CreateSampleImage ----------------------------------------------------------------------------*/
  122. //
  123. //    This function creates  primitive shapes and adds them to the window's page shape.
  124. //
  125. void CreateSampleImage(WindowPtr wind)
  126. {
  127.     gxShape            thePage;
  128.     gxShape         theLine;
  129.     gxLine            lineData = {ff(25), ff(25), ff(125), ff(125)};
  130.     gxShape            theRect;
  131.     gxRectangle     rectData = {ff(25), ff(25), ff(75), ff(75)};    
  132.     gxShape         theCurve;
  133.     gxCurve         curveData = {ff(25), ff(25), ff(275), ff(75), ff(125), ff(125)};    
  134.     gxShape         thePath;
  135.     long             tripleEightData[] = {    1 /* # of contours */, 
  136.                                             6 /* # of points */, 
  137.                                             0xff000000,
  138.                                                0, 0,  // the points 
  139.                                                ff(75),  0, 
  140.                                                ff(5), ff(50),
  141.                                                ff(75),  ff(100),
  142.                                                0,  ff(100), 
  143.                                                ff(75), ff(50)};        
  144.     gxShape         theText;
  145.     gxRectangle     theTextBounds;
  146.     gxColor         textColor;
  147.     Fixed            x,y;
  148.     short            loop;
  149.     gxShape         thePolygon;
  150.     long starData[] = {    1,  /**  number of contours  **/
  151.                          5 , /**  number of points  **/
  152.                         ff(60), 0, ff(90), ff(90),  ff(0), ff(30),  ff(120), ff(30), ff(0), ff(90)};   /**  the points  **/
  153.     gxShape     theBitmap;
  154.  
  155. // Retrieve the page shape so we can add to it.
  156.  
  157.     thePage = GetDocShape(wind);
  158.  
  159.  
  160. // Create a line
  161.     
  162.     theLine = GXNewLine (&lineData);
  163.  
  164.     AddToShape(thePage, theLine);
  165.     GXDisposeShape(theLine);  
  166.  
  167.  
  168. // Create a rectangle which is: red & is draw with it's frame.
  169.  
  170.     theRect = GXNewRectangle(&rectData); 
  171.      SetShapeCommonColor (theRect, red);
  172.     GXSetShapeFill (theRect, gxClosedFrameFill);
  173.     GXMoveShapeTo (theRect,  ff(150), ff(25));
  174.  
  175.     AddToShape(thePage, theRect);
  176.     GXDisposeShape(theRect);  
  177.  
  178.  
  179. // Create a curve which has: a 3.25 pen thickness
  180.     
  181.     theCurve = GXNewCurve(&curveData); 
  182.     
  183.     // The fl marco converts floating gxPoint #'s to fixed gxPoint.
  184.     GXSetShapePen(theCurve, fl(3.25));
  185.     GXMoveShapeTo (theCurve,  ff(210), ff(25));
  186.  
  187.     AddToShape(thePage, theCurve);
  188.     GXDisposeShape(theCurve);  
  189.  
  190.  
  191. // Create apath which has: a 2 pen thickness, it's gxColor is green, and it's drawn with it's frame 
  192.                                                                
  193.     thePath = GXNewPaths((gxPaths *) tripleEightData);
  194.     GXSetShapeFill (thePath, gxClosedFrameFill);
  195.      GXSetShapePen(thePath, ff(2));
  196.     SetShapeCommonColor (thePath, green);
  197.  
  198.     GXMoveShapeTo (thePath,  ff(390), ff(25));
  199.  
  200.     AddToShape(thePage, thePath);
  201.     GXDisposeShape(thePath);  
  202.  
  203.  
  204. // Create a character S which is: colored in hsv space and it is rotated 15 degrees - six times via the left bottom corner.
  205. // Create the text, set the gxFont size, and set the gxFont name
  206.  
  207.     theText = GXNewText(1,(unsigned char*)"S",  nil);
  208.     SetShapeCommonFont(theText, timesFont);
  209.     GXSetShapeTextSize(theText, ff(200));
  210.     GXMoveShapeTo (theText,  ff(25), ff(275));
  211.     GXSetShapeAttributes (theText,  gxMapTransformShape);
  212.  
  213. // Create an hsv color space and set up the initial colors
  214.  
  215.     textColor.space = gxHSVSpace;
  216.     textColor.profile = nil;
  217.     textColor.element.hsv.hue = 0x7400;
  218.     textColor.element.hsv.saturation = 0xFFFF;
  219.     textColor.element.hsv.value = 0xFFFF;
  220.  
  221. // Get the bounds of "theText" and determine the bottom left corner
  222.  
  223.     GXGetShapeBounds(theText, 0L, &theTextBounds);
  224.     x = theTextBounds.left;
  225.     y = theTextBounds.bottom;
  226.  
  227. // Rotate "theText" 15 degrees - 6 times. Add each letter to the picture.
  228.  
  229.     for (loop = 0; loop < 6; loop++) {
  230.         GXSetShapeColor(theText, &textColor);
  231.         GXRotateShape(theText, ff(15), x, y);
  232.     
  233.         AddToShape(thePage, theText);
  234.  
  235.         textColor.element.hsv.hue += 0x0940;
  236.     }
  237.     
  238.     GXDisposeShape(theText);  
  239.     
  240.                 
  241. // Create a polygon which has the following features: yellow, drawn with a pen = 3, and skew it in the vertical direction by 0.5 
  242.  
  243.     thePolygon = GXNewPolygons((gxPolygons *) starData);
  244.     GXSetShapeFill(thePolygon, gxEvenOddFill);
  245.     GXSetShapePen (thePolygon, ff(3));
  246.     SetShapeCommonColor (thePolygon, yellow);
  247.     GXMoveShapeTo (thePolygon,  ff(240), ff(110));
  248.     GXSkewShape(thePolygon, 0, fl(0.5), 0, 0);
  249.     
  250.     AddToShape(thePage, thePolygon);
  251.     GXDisposeShape(thePolygon);  
  252.  
  253.  
  254. // Retrieve a bitmap from the resource fork and skew it in the horizontal direction by 2.
  255.     
  256.     theBitmap = GetPixMapShape(128);
  257.     GXValidateShape (theBitmap);
  258.  
  259.     GXSkewShape(theBitmap, ff(2), 0, 0, 0);
  260.     GXMoveShapeTo (theBitmap,  ff(290), ff(190));
  261.     
  262.     AddToShape(thePage, theBitmap);
  263.     GXDisposeShape(theBitmap);  
  264.  
  265.  
  266. // Invalidate the window's portRect so that everything gets updated.
  267.     
  268.     SetPort(wind);
  269.     InvalRect(&wind->portRect);
  270. }
  271.  
  272.  
  273. /*------ DoCreateNew ---------------------------------------------------------------------------------*/
  274. //
  275. //    This routine is called when a window needs to be created.
  276. //
  277. OSErr DoCreateNew(void)
  278. {
  279.     OSErr        err;
  280.     WindowPtr    wind;
  281.     
  282.     /** Create a window, attach a default gxViewPort to it, create and gxInitialize our private data for it, and
  283.     /** add a sample image to its page shape. **/
  284.  
  285.     wind = NewWindow(nil, &gWindowQDRect, gWindowTitle, true, noGrowDocProc, (WindowPtr)-1L, true, 0L);
  286.     
  287.     if (!wind)
  288.         err = MemError();
  289.     else
  290.     {
  291.         SetDefaultViewPort(GXNewWindowViewPort(wind));            
  292.         err = DoInitialization(wind);
  293.     }
  294.     
  295.     CreateSampleImage(wind);
  296.     return err;
  297. }
  298.  
  299.  
  300. /*------ DoDispose -------------------------------------------------------------------------------------*/
  301. //
  302. //    This routine is called when a window needs to be disposed of.
  303. //
  304. void DoDispose(wind)
  305. WindowPtr wind;
  306. {
  307.     TH_Doc    doc;
  308.     
  309.     /**  
  310.         You should always dispose of your GX graphics objects before tossing your window. Why? It's generally good 
  311.         form and this approach guarantees that everything is disposed. If you had not disposed of everything, the
  312.         call to DisposeWindow should dispose of the objects. If you are running the debugging version of the 
  313.         SecretGraphics init with notices set, you will receive a notice that you had not disposed of everything. You
  314.         can turn notices on in this file by setting gDebugging = TRUE (above).
  315.     **/
  316.     
  317.     doc = (TH_Doc)  GetWRefCon(wind);        // Remember, this is where we stored our private data.
  318.     
  319.     GXDisposeShape(GetDocShape(wind));         // Dispose of this doc's shape.
  320.     GXDisposeJob(GetDocJob(wind));            // Dispose of this doc's print job.
  321.     DisposHandle((Handle) doc);            // Dispose of our private data.
  322.  
  323.        DisposeWindow(wind);                // Dispose of the window.
  324. }
  325.  
  326.  
  327. /*------ DoIdle ----------------------------------------------------------------------------------------*/
  328. //
  329. //    This routine is called to do things while we're idling through tthe event loop.
  330. //
  331. void DoIdle(WindowPtr wind)
  332. {
  333. }
  334.  
  335.